﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGioiThieu.Models;

namespace WebGioiThieu.Areas.Admin.Controllers
{ 
    public class MenuTypeManagerController : Controller
    {
        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();

        //
        // GET: /Admin/MenuTypeManager/
        [CustomAuthorize]
        public ViewResult Index()
        {
            return View(db.MenuTypes.ToList());
        }

        //
        // GET: /Admin/MenuTypeManager/Details/5
        [CustomAuthorize]
        public ViewResult Details(int id)
        {
            MenuType menutype = db.MenuTypes.Find(id);
            return View(menutype);
        }

        //
        // GET: /Admin/MenuTypeManager/Create
        [CustomAuthorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Admin/MenuTypeManager/Create
        [CustomAuthorize]
        [HttpPost]
        public ActionResult Create(MenuType menutype)
        {
            if (menutype.BiDanh == null)
                menutype.BiDanh = menutype.Ten.Replace(" ", "-");
            if (ModelState.IsValid)
            {
                db.MenuTypes.Add(menutype);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(menutype);
        }
        
        //
        // GET: /Admin/MenuTypeManager/Edit/5
 [CustomAuthorize]
        public ActionResult Edit(int id)
        {
            MenuType menutype = db.MenuTypes.Find(id);
            return View(menutype);
        }

        //
        // POST: /Admin/MenuTypeManager/Edit/5
        [CustomAuthorize]
        [HttpPost]
        public ActionResult Edit(MenuType menutype)
        {
            if (menutype.BiDanh == null)
                menutype.BiDanh = menutype.Ten.Replace(" ", "-");
            if (ModelState.IsValid)
            {
                db.Entry(menutype).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(menutype);
        }

        //
        // GET: /Admin/MenuTypeManager/Delete/5
 [CustomAuthorize]
        public ActionResult Delete(int id)
        {
            MenuType menutype = db.MenuTypes.Find(id);
            return View(menutype);
        }

        //
        // POST: /Admin/MenuTypeManager/Delete/5
        [CustomAuthorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(int id,FormCollection Collection)
        {            
            MenuType menutype = db.MenuTypes.Find(id);
            db.MenuTypes.Remove(menutype);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}